<?php

namespace common\models\user;

use common\helpers\IpHelper;
use Yii;
use yii\db\Exception;

/**
 * This is the model class for table "{{%user_account}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $type 货币类型
 * @property int $current 当前账户积分
 * @property int $amount 历史累积
 * @property int $consume 历史消费
 * @property int $frozen 历史冻结
 * @property int $status 账户状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at
 */
class UserAccount extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    use \common\traits\relation\UserData;

    const STATUS_FORZEN = -1;
    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;
    //
    const TYPE_EXP = 0;
    const TYPE_CREDIT = 1;
    const TYPE_GOLD = 2;
    const TYPE_CASH = 3;
    //
    const BALANCE_IN = 1;
    const BALANCE_OUT = -1;
    const BALANCE_FROZEN = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%user_account}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'type', 'current', 'amount', 'consume', 'frozen', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'type' => '货币类型',
            'current' => '当前账户积分',
            'amount' => '历史累积',
            'consume' => '历史消费',
            'frozen' => '历史冻结',
            'status' => '账户状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => 'Deleted At',
        ];
    }

    public function getLogs() {
        return $this->hasMany(UserAccountLog::class, ['account_id' => 'id'])->orderBy([
            'created_at' => SORT_DESC
        ]);
    }

    /**
     * 账户状态是否有效
     *
     * @return bool
     */
    public function isAvaliable() {
        if ($this->status == static::STATUS_ACTIVE) {
            return true;
        }
        return false;
    }

    public function getConsoles() {
        $consoles = [];
        if ($this->status == static::STATUS_DEFAULT) {
            $consoles[] = [
                'label' => '激活账户',
                'url' => ['/user/account/active', 'id' => $this->id],
                'options' => [
                    'class' => 'btn-light-primary'
                ]
            ];
            $consoles[] = [
                'label' => '冻结账户',
                'url' => ['/user/account/frozen', 'id' => $this->id],
                'options' => [
                    'class' => 'btn-light-danger',
                    'data-confirm' => '冻结用户账户后用户将不可以再使用积分和获得积分，继续操作?',
                    'data-method' => 'POST'
                ]
            ];
        }

        if ($this->status == static::STATUS_FORZEN) {
            $consoles[] = [
                'label' => '解冻',
                'url' => ['/user/account/thaw', 'id' => $this->id],
            ];
        }

        if ($this->status == static::STATUS_ACTIVE) {
            $consoles[] = [
                'label' => '冻结账户',
                'url' => ['/user/account/frozen', 'id' => $this->id],
                'options' => [
                    'class' => 'btn-light-danger',
                    'data-confirm' => '冻结用户账户后用户将不可以再使用积分和获得积分，继续操作?',
                    'data-method' => 'POST'
                ]
            ];
        }
        return $consoles;
    }

}
